const path = require('path')
const os = require('os')

// 主配置
module.exports = {
  lintOnSave: false,
  // publicPath: "/orders-accept/",
  // outputDir: "orders-accept",
  // assetsDir: 'static',
  css: {
    sourceMap: false,
    loaderOptions: {
      scss: {
        prependData: '@import "./src/styles/index.scss";'
      }
    }
  },
  devServer: {
    host: getNetworkIp(), // 设置启动项目网址
    port: 8081, // 设置启动项目端口号
    https: false, // 是否使用https协议
    open: true, // 设置是否自动打开浏览器
    hotOnly: false, // 是否开启热更新
    proxy: {
      // 配置跨域
      'http://localhost:8081/order/query': {
        target: 'http://192.168.1.71:8085',
        changeOrigin: true,
        ws: true
        // pathRewrite: {
        //   "^/order/query": "/order/query"
        // }
      }
    }
  },
  configureWebpack: {
    name: '电信CMR3.0'
  },
  chainWebpack (config) {
    config.module.rule('svg').exclude.add(resolve('./src/assets/icons/svg'))

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('./src/assets/icons/svg'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({ symbolId: 'icon-[name]' })
  }
}

/**
 * 相对路径转绝对路径
 * @param {} dir
 * @returns
 */
function resolve (dir) {
  return path.join(__dirname, dir)
}
/**
 * 获取本地局域网IP
 * @returns
 */
function getNetworkIp () {
  let needHost = ''
  try {
    const network = os.networkInterfaces()
    for (const dev in network) {
      const iface = network[dev]
      for (let i = 0; i < iface.length; i++) {
        const alias = iface[i]
        if (
          alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal
        ) {
          needHost = alias.address
        }
      }
    }
  } catch (e) {
    needHost = 'localhost'
  }
  return needHost
}
